package org.example.alyun;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.time.StopWatch;

import java.util.Random;
import java.util.concurrent.TimeUnit;

public class AddCartAd {

  public static void baseCart(String cookie, String XFS, Integer cartTotal, Integer rand,  String json) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    String URL = "https://cart.aliyun.com/cart/addToCart";
    Integer cartTotalQty = 1;
    while (true) {
      String result2 = HttpRequest.post(URL)
        .cookie(cookie)
        .header("X-XSRF-TOKEN", XFS)
        .body(json)
        .execute().body();

      JSONObject jsonObject = JSONObject.parseObject(result2);
      if ("200".equals(jsonObject.getString("code"))) {
        if (cartTotalQty >= cartTotal) {
          break;
        }

      } else {
        break;
      }
      try {
        cartTotalQty++;
        Random random = new Random();
        int randomNumber = random.nextInt(rand) + 6;
        System.out.println("----randomNumber------" + randomNumber + "------" + cartTotalQty);
        TimeUnit.SECONDS.sleep(randomNumber);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

    }
    System.out.println("------------结束------");
    stopWatch.stop();
  }


}
